<?php
/**
 * @Auth: wonli <wonli@live.com>
 * Panel.php
 */
namespace app\admin\controllers;
use modules\admin\FinanceModule;
use modules\admin\UserModule;

class Panel extends Admin
{
    /**
     * 登录成功后默认跳转到空白的面板
     */
    function index()
    {
        $this->display($this->data);
    }

    /**
     * 用户信息设置
     */
    function user_info()
    {

        #注册用户模块
        $U_Info = new UserModule();

        #获取个人年度营业额
        $this->data['Y_Price'] = $U_Info->User_Wastebook(
            array(
                'name' => $_SESSION['uname'],
                'start' => 1,
                'time' => array('like',date('Y%'))
            )
        );

        #获取个人月度营业额
        $this->data['M_Price'] = $U_Info->User_Wastebook(
            array(
                'name' => $_SESSION['uname'],
                'start' => 1,
                'time' => array('like',date('Y-m%'))
            )
        );

        #获取登录次数
        $this->data['Loing_Num'] = $U_Info->User_Login_Num();

        #获取未完成任务数量
        $this->data['Calendar_N_Num'] = $U_Info->User_Calendar(array('start' => 0));

        #获取已完成任务数量
        $this->data['Calendar_Y_Num'] = $U_Info->User_Calendar(array('start' => 1));

        #年度销售额排名
        $this->data['Wastebook_Rank_Y'] = $U_Info->User_Wastebook_Rank(
            array(
                'start' => 1,
                'time' => array('like',date('Y%')),
            )
        );

        #月度销售额排名
        $this->data['Wastebook_Rank_M'] = $U_Info->User_Wastebook_Rank(
            array(
                'start' => 1,
                'time' => array('like',date('Y-m%'))
            )
        );

        $t = date("m") - 1;

        if ($t < 10){
            $t = "0".$t;
        }

        //销售
        $u_name = array(
            '韦伟',
            '林柳强',
            '左良锑',
            '廖荣华'
        );

        #上个月提成
        if ($_SESSION['uid'] == 5){

            $this->data['price_opc'] = $this->Percentage_Count('左良锑',$t,"pc");

            $this->data['price_ojk'] = $this->Percentage_Count('左良锑',$t,"jk");

            $this->data['price_oall'] = $this->Percentage_Count('左良锑',$t,"all");

        }
        else{

            $this->data['price_opc'] = $this->Percentage_Count($_SESSION['uname'],$t,"pc");

            $this->data['price_ojk'] = $this->Percentage_Count($_SESSION['uname'],$t,"jk");

            $this->data['price_oall'] = $this->Percentage_Count($_SESSION['uname'],$t,"all");

        }

        #本月提成
        $this->data['price_pc'] = $this->Percentage_Count($_SESSION['uname'],date("m"),"pc");

        $this->data['price_jk'] = $this->Percentage_Count($_SESSION['uname'],date("m"),"jk");

        $this->data['price_all'] = $this->Percentage_Count($_SESSION['uname'],date("m"),"all");





        /**
         * 文件上传模型
         *
         * Array (
         * [avatars] => Array (
         *              [name] => ysfl.png #文件名字
         *              [type] => image/png #文件类型
         *              [tmp_name] => /tmp/phpAvtcQv #临时名字
         *              [error] => 0 #上传失败的错误代码
         *              [size] => 11933 #文件大小
         *  )
         * )
         *
         */

        //获得图片所有数据
        $img = $_FILES['avatars'];

        //检测提交图片后是否成功上传到服务器临时文件夹
        if($this->is_post()){

            $this->data['waring'] = $U_Info->User_Avatars($img);

        }

        $this->display($this->data);
    }

    /**
     * 财务信息
     */
    function Finance()
    {
        #注册用户模块
        $F = new FinanceModule();

        $t = 31;

        $this->data['test'] = $F->Finance_Day_Query();


        $this->display($this->data);
    }

    /**
     * 提成计算
     */
    function Percentage_Count($name,$t,$type)
    {
        #注册用户模块
        $Percentage = new UserModule();

        switch($type)
        {
            case "pc": #电脑配件提成
                return $Percentage->User_Wastebook(
                    array(
                        'name' => $name,
                        'start' => 1,
                        'time' => array('like',"2016-$t%"),
                        'type' => array(
                            'or',
                            array(
                                '电脑配件',
                                '显卡',
                                '显示器',
                                '三大件'
                            )
                        )
                    )
                );
                break;

            case "jk": #监控配件提成
                return $Percentage->User_Wastebook(
                    array(
                        'name' => $name,
                        'start' => 1,
                        'time' => array('like',"2016-$t%"),
                        'type' => '监控配件'
                    )
                );
                break;

            case "all": #其他提成
                $sda = "
                        name = '".$name."' and
                        start = 1 and
                        time like '2016-$t%' and
                        type != '电脑配件' and
                        type != '三大件' and
                        type != '显卡' and
                        type != '显示器' and
                        type != '监控配件'
                        ";
                return $Percentage->User_Wastebook($sda);
                break;
        }
    }
}
